package com.hy.frame.recycler;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.hy.frame.adapter.IAdapterListener;
import com.hy.frame.adapter.IAdapterLongListener;

import java.util.List;

/**
 * title BaseAdapter for RecyclerView
 * author HeYan
 * time 2020/11/30 15:09
 * desc 无
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseHolder> implements IBaseAdapter<T> {
    private final Context mCxt;
    private List<T> mDatas;
    private IAdapterListener<T> mListener;
    private View.OnClickListener mClickListener;
    private View.OnLongClickListener mLongClickListener;
    public static final int ADAPTER_POSITION = 0xFF000001;
    public static final int TYPE_ITEM = 0;

//    public Context getContext() {
//        return mCxt;
//    }
//
//    public List<T> getDatas() {
//        return mDatas;
//    }
//
//    public IAdapterListener<T> getListener() {
//        return mListener;
//    }
//
//    public void setListener(IAdapterListener<T> mListener) {
//        this.mListener = mListener;
//    }

    public BaseAdapter(@NonNull Context cxt) {
        this(cxt, null);
    }

    public BaseAdapter(@NonNull Context cxt, @Nullable List<T> datas) {
        this(cxt, datas, null);
    }

    public BaseAdapter(@NonNull Context cxt, @Nullable List<T> datas, @Nullable IAdapterListener<T> listener) {
        super();
        this.mCxt = cxt;
        this.mDatas = datas;
        this.mListener = listener;
    }
    //----------IBaseAdapter START---------------------------------

    @Override
    public Context getContext() {
        return this.mCxt;
    }

    @Override
    public List<T> getDatas() {
        return this.mDatas;
    }

    @Override
    public int getDataCount() {
        return this.mDatas == null ? 0 : this.mDatas.size();
    }

    @Override
    public boolean isBindDataId() {
        return false;
    }

    @Override
    public int getDataId(int position) {
        return position;
    }

    @Override
    public T getDataItem(int position) {
        if (position < getDataCount())
            return this.mDatas.get(position);
        return null;
    }

    @Override
    public IAdapterListener<T> getListener() {
        return this.mListener;
    }

    public void setListener(@Nullable IAdapterListener<T> listener) {
        this.mListener = listener;
    }

    @Override
    public IAdapterLongListener<T> getLongListener() {
        if (getListener() != null && getListener() instanceof IAdapterLongListener<?>) {
            return (IAdapterLongListener<T>) getListener();
        }
        return null;
    }

    @Override
    public void setOnClickListener(View v, int position) {
        if (v == null) return;
        if (this.mClickListener == null) {
            this.mClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (int) v.getTag(ADAPTER_POSITION);
                    if (getListener() != null) {
                        getListener().onViewClick(v, getDataItem(position), position);
                    }
                }
            };
        }
        v.setTag(ADAPTER_POSITION, position);
        v.setOnClickListener(this.mClickListener);
    }

    @Override
    public void setOnLongClickListener(View v, int position) {
        if (v == null) return;
        if (this.mLongClickListener == null) {
            this.mLongClickListener = new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = (int) v.getTag(ADAPTER_POSITION);
                    if (getListener() != null && getListener() instanceof IAdapterLongListener<?>) {
                        ((IAdapterLongListener<T>) getListener()).onViewLongClick(v, getDataItem(position), position);
                    }
                    return true;//拦截点击事件
                }
            };
        }
        v.setTag(ADAPTER_POSITION, position);
        v.setOnLongClickListener(this.mLongClickListener);
    }

    @Override
    public void refresh() {
        this.notifyDataSetChanged();
    }

    @Override
    public void refresh(List<T> datas) {
        this.mDatas = datas;
        refresh();
    }

    @Override
    public View inflate(int resId) {
        return View.inflate(getContext(), resId, null);
    }

//    @Override
//    public int getItemLayoutId() {
//        return 0;
//    }

    @Override
    public View getItemView() {
        return inflate(getItemLayoutId());
    }

    @Override
    public BaseHolder getItemHolder(View v) {
        return new BaseHolder(v);
    }

//    @Override
//    public void bindItemData(BaseHolder holder, int position) {
//
//    }

    //----------IBaseAdapter END---------------------------------

    /**
     * 默认采用数据的长度
     */
    @Override
    public int getItemCount() {
        return getDataCount();
    }

    @Override
    public int getCurPosition(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return TYPE_ITEM;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return getItemHolder(getItemView());
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHolder holder, int position) {
        if (isBindDataId()) {
            holder.itemView.setId(getDataId(position));
        }
        bindItemData(holder, position);
    }

}
